JR.container = function($){
	return {
		init : function(){
			JR.container.list.init();
			JR.container.add.init();
			JR.container.switcher.init();
			JR.container.view.init();
			JR.container.link.init();
		}
	};
}(jQuery);

JR.container.list = function($){
	
	var ContainerModel = function(){};
	ContainerModel.prototype = {
		setId : function(id){
			this.id = id;
		},
		setName : function(name){
			this.name = name;
		},
		setDesc : function(desc){
			if(desc == "" || desc ==null){
				desc = '无';
			}
			this.desc = desc;
		},
		setUrl : function(url){
			this.url = url;
		},
		setSurl : function(surl){
			this.surl = surl;
		},
		setTurl : function(turl){
			this.turl = turl;
		},
		getId : function(){
			return this.id;
		},
		getName : function(){
			return this.name;
		},
		getDesc : function(){
			return this.desc;
		},
		getUrl : function(){
			return this.url;
		},
		getSurl : function(){
			return this.surl;
		},
		getTurl : function(){
			return this.turl;
		}
	};
	
	Array.prototype.clear=function(){  
		this.length=0;  
	};
	
	var ContainersModel = function(){
		this.containers = new Array();
	};
	ContainersModel.prototype = {
		add : function(params){
			var c = new ContainerModel();
			c.setId(params.id);
			c.setName(params.name);
			c.setDesc(params.desc);
			c.setUrl(params.url);
			c.setSurl(params.surl);
			c.setTurl(params.turl);
			this.containers.push(c);
		},
		count : function(){
			return this.containers.length;
		},
		get : function(index){
			return this.containers[index];
		},
		clear : function(){
			this.containers.clear();
		}
	};
	
	var model = new ContainersModel();
	
	var view = {
		init : function(){
			this.box = $('#box');
			this.preload();
		},
		preload : function(){
			this.box.empty();
			this.box.addClass('preload loading');
			model.clear();
		},
		render : function(){
			this.box.removeClass('preload loading');
			if(model.count()==0){
				this.promptEmpty();
				return;
			}
			for(var index=0; index < model.count(); index++){
				this.buildContainer(model.get(index));
			}
			this.addClear();
		},
		promptEmpty : function(){
			$('<div>').addClass('empty').text('目前该类别下没有集装箱').appendTo(this.box);
		},
		buildContainer : function(model){
			var container = $('<div>').addClass('container').appendTo(this.box);
			var container_img = $('<div>').addClass('container-img').appendTo(container);
			container_img.attr('href', JR.statics.contextPath + model.getTurl());
			$.imgAuto().load(JR.statics.contextPath + model.getTurl(), 100, 100).appendTo(container_img);
			var container_name = $('<div>').addClass('container-name').text(model.getName()).appendTo(container);
			JR.utils.hideMore(container_name, 14);
			
			container.hover(function(){
				container.addClass('container-hover');
				container_img.addClass('container-img-hover');
				container_name.addClass('container-name-hover');
			},function(){
				container.removeClass('container-hover');
				container_img.removeClass('container-img-hover');
				container_name.removeClass('container-name-hover');
			});
			
			container.click(function(){
				JR.container.switcher.showView();
				JR.container.view.show(model);
			});
		},
		addClear : function(){
			$('<div>').addClass('clear').appendTo(this.box);
		}
	};
	
	var controller = {
		init : function(){
			JR.ajax(JR.url2('container-list-exec.action'), this.analyzeData, {id: JR.statics.id});
		},
		analyzeData : function(data){
			var containers = data.data;
			for(var index=0; index < containers.length; index++){
				model.add({
					id: containers[index].id,
					name: containers[index].name,
					desc: containers[index].description,
					url: containers[index].url,
					surl: containers[index].surl,
					turl: containers[index].turl
				});
			}
			
			view.render();
		}
	};
	
	return {
		init : function(){
			view.init();
			controller.init();
		}
	};	
}(jQuery);

JR.container.view = function(){
	var view = {
		init : function(){
			this.container = $('#view');
			this.image = $('#image', this.container);
			this.name = $('#name .right', this.container);
			this.desc = $('#desc .right', this.container);
		},
		show : function(model){
			JR.container.link.render(model);
			
			this.image.empty();
			this.name.empty();
			this.desc.empty();
			
			this.name.text(model.getName());
			this.desc.text(model.getDesc());
			this.desc.prepend($('<div>').addClass('min-height'));
			$.imgAuto().load(JR.statics.contextPath + model.getSurl(), 300, 300).appendTo(this.image);
		}
	};
	
	return {
		init : function(){
			view.init();		
		},
		show : function(model){
			view.show(model);
		}
	};
}(jQuery);

JR.container.link = function(){
	
	var view = {
		init : function(){
			this.viewContainer = $('#view');
			this.container = $('.operation', this.viewContainer);
			this.id = $('#cid', this.container);
			this.edit = $('#edit', this.container);
			this.del = $('#del', this.container);
			this.msg = $('#view-msg', this.container);
			this.addEvents();
		},
		addEvents : function(){
			var th = this;
			this.del.click(function(){
				var result = confirm('确定要删除此集装箱吗？');
				if(result){
					th.delContainer();
				}
			});
			this.edit.click(function(){
				th.editContainer();
			});
			this.msg.click(function(){
				th.viewMsg();
			});
		},
		render : function(model){
			this.id.val(model.getId());
			this.viewContainer.unblock();
		},
		getId : function(){
			return this.id.val();
		},
		delContainer : function(){
			this.viewContainer.block({
				message:null
			});
			var th = this;
			JR.ajax(JR.url2('container-delete.action'), function(){th.afterDel()}, {id: this.getId()});
		},
		afterDel : function(){
			this.viewContainer.unblock();
			this.viewContainer.block({
				message: '删除成功!<br/>页面正在跳转中...'
			});
			setTimeout(function(){
				JR.container.switcher.showList();
				JR.container.list.init();
			}, 1000);
		},
		editContainer : function(){
			location.href = JR.url2('container-edit.action?id=' + this.getId());
		},
		viewMsg : function(){
			alert('查看留言' + this.getId());
		}
	};
	
	return {
		init : function(){
			view.init();	
		},
		render : function(model){
			view.render(model);
		}
	};
}(jQuery);

JR.container.add = function(){
	
	var add = {
		init : function(){
			this.container = $('#bread');
			this.initBackButton();
			this.initAddButton();
		},
		initAddButton : function(){
			var add = $('<div>').addClass('add').text('添加集装箱').appendTo(this.container);
			add.wrap($('<a>').attr('href', JR.url2('container-add.action?id=') + JR.statics.id));
		},
		initBackButton : function(){
			var back = $('<div>').addClass('back').text('返回').appendTo(this.container);
			back.click(function(){
				JR.container.switcher.showList();
			});
			back.hide();
		}
	};
	
	return {
		init : function(){
			add.init();	
		}
	};
}(jQuery);

JR.container.switcher = function(){
	
	var view = {
		init : function(){
			this.list = $('#box');
			this.view = $('#view');
			this.back = $('#bread .back');
		},
		showView : function(){
			var th = this;
			th.list.fadeOut(function(){
				th.view.fadeIn();
				th.back.fadeIn();
				
			});
		},
		showList : function(){
			var th = this;
			th.back.fadeOut();
			th.view.fadeOut(function(){
				th.list.fadeIn();
			});
		}
	};
	
	return {
		init : function(){
			view.init();
		},
		showView : function(){
			view.showView();
		},
		showList : function(){
			view.showList();
		}
	};
}(jQuery);

$(document).ready(function() {
	JR.container.init();
});